// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
const $ = db.command.aggregate;

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  let source = event.source;
  let card_number = event.card_number;
  let card_password = event.card_password;
  let express_number = event.express_number;
  let card_id = event.card_id;
  let update_time = new Date();
  if (source == 'scan') {
    let card_info = await db.collection('ops_card').where({
      card_number: card_number,
      card_password: card_password,
    }).get();
    if (card_info.data.length == 0) {
      return "卡券不存在";
    } else {
      card_id = card_info.data[0]._id;
      Issuer_openid = card_info.data[0].Issuer_openid;
      if (Issuer_openid != wxContext.OPENID) {
        return "不是本账号所签发的卡券";
      } else {
        let order_info = await db.collection('ops_order').where({
          card_id: card_info.data[0]._id,
        }).get();
        let goods_info = await db.collection('ops_goods').where({
          _id: card_info.data[0].goods_id,
        }).get();
        updateTable(goods_info.data, order_info.data);
      }
    }
  } else {
    let card_info = await db.collection('ops_card').where({
      _id: card_id
    }).get();
    let order_info = await db.collection('ops_order').where({
      card_id: card_info.data[0]._id,
    }).get();
    let goods_info = await db.collection('ops_goods').where({
      _id: card_info.data[0].goods_id,
    }).get();
    console.log(card_info);
    console.log(order_info);
    console.log(goods_info);
    updateTable(goods_info.data, order_info.data);
  };
  return 1;
  function updateTable(goods_info, order_info) {
    // ops_card增加快递单号和更新时间
    db.collection('ops_card').where({
      _id: card_id,
    }).update({
      data: {
        express_number: express_number,
        status: 4, // 已发货
        update_time: update_time,
      },
    }).then(res => {
      // ops_order增加快递单号和更新时间
      db.collection('ops_order').where({
        card_id: card_id,
      }).update({
        data: {
          express_number: express_number,
          status: 2, // 已发货
          update_time: update_time,
        },
      })
    }).then(res => {
      console.log('order_info[0].user_openid:'+order_info[0].user_openid);
      cloud.openapi.subscribeMessage.send({
        // "miniprogram_state": 'developer',
        "touser": order_info[0].user_openid,
        "page": 'pages/order/index?TabCur=1',
        "lang": 'zh_CN',
        "templateId": 'jlF85j8mQfZi6lLtr2zLKfxP7GX0_rOGhSnuiCL8gFU',
        "data": {
          "character_string1": {
            "value": order_info[0]._id,
          },
          "thing2": {
            "value": goods_info[0].goods_name,
          },
          "character_string9": {
            "value": express_number,
          },
          "time11": {
            "value": update_time.getFullYear() + '.' + (update_time.getMonth() + 1) + '.' + update_time.getDate() + ' ' + update_time.getHours() + ':' + update_time.getMinutes(),
          },
          "thing17": {
            "value": '点击卡片可查看物流信息'
          }
        }
      })
    }).catch(err => console.error(err));
  }
}